//
//  YouHuiJuanViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/6.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "YouHuiJuanViewController.h"

@interface YouHuiJuanViewController ()

@end

@implementation YouHuiJuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"选择优惠劵";
    self.tabBarController.tabBar.hidden = YES;
    [self getYouHuiJuan];
    if (!self.my) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(wangCheng)];

    }
    
}
- (void)wangCheng{
  
    
    if (self.datasArr.count!=0) {
          NSDictionary * dic =[self.datasArr objectAtIndex:self.index];
        if (self.price) {
            self.price(dic);

        }
    }
    
    
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)getYouHuiJuan{
    MBProgressHUD * hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"数据加载中";
    
    [QingQiuShuJu getWoDeYouHuiJuan:[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"] Block:^(NSDictionary *dic, BOOL isOK)
     {
         if (isOK) {
             
             if ([[dic valueForKey:@"status"]intValue]==200) {
                 
                 self.datasArr = [dic valueForKey:@"data"];
                 
                 [self.tableView reloadData];
                 
                 [hud setHidden:YES];
                 hud.removeFromSuperViewOnHide =YES;
             }
             else{
                 
                 hud.mode = MBProgressHUDModeText;
                 hud.labelText = [dic valueForKey:@"msg"];
                 hud.removeFromSuperViewOnHide = YES;
             }
         }
         else{
             hud.mode = MBProgressHUDModeText;
             hud.labelText = @"网络连接错误";
             
             hud.removeFromSuperViewOnHide = YES;
         }
         
     }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YouHuiTableViewCell" bundle:nil] forCellReuseIdentifier:@"youhui"];
    
    self.index = -1;
    

}
#pragma mark --- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datasArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YouHuiTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"youhui" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    NSDictionary * dic =[self.datasArr objectAtIndex:indexPath.row];
    
    
    [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:[dic valueForKey:@"imgUrl"]] placeholderImage:[UIImage imageNamed:@"like"]];
    cell.lable1.text = [NSString stringWithFormat:@"x%@",[dic valueForKey:@"num"]];
    
    if ( self.index ==indexPath.row) {
        
        cell.imageView2.image = [UIImage imageNamed:@"duigou1"];
        
    }
    else{
        
        cell.imageView2.image =[UIImage imageNamed:@"huiduigou"];
    }
    return cell;

}
#pragma mark --- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
 
    
    self.index =indexPath.row;
    
  [self.tableView reloadData];
    
    
}
- (IBAction)YouHuiJuan:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    switch (sender.tag) {
        case 0:
        {
        
        }
            break;
        case 1:
        {
            
        }
            break;
        case 2:
        {
            
        }
            break;
        case 3:
        {
            
        }
            break;
        default:
            break;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
